package br.pucminas.dao;

import java.util.List;

import javax.persistence.Query;

import br.pucminas.models.Editora;

public class EditoraDAO  extends GenericDAO<Long, Editora>{

	private static final long serialVersionUID = -5444003959172788087L;

	@SuppressWarnings("unchecked")
	public List<Editora> buscaEditoras(){
		Query query = super.getEntityManager().createQuery("SELECT e FROM Editora e");
		List<Editora> editoras = (List<Editora>) query.getResultList();
		return editoras;
	}
	
	public Editora inserirEditora(Editora editora) throws Exception{
		super.getEntityManager().getTransaction().begin();
		super.getEntityManager().persist(editora);
		super.getEntityManager().getTransaction().commit();
		return editora;
	}
	
	public Editora atualizarEditora(Editora editora) throws Exception{
		super.getEntityManager().getTransaction().begin();
		super.getEntityManager().merge(editora);
		super.getEntityManager().getTransaction().commit();
		return editora;
	}
	
	public void excluirEditora(Editora editora) throws Exception{
		super.getEntityManager().getTransaction().begin();
		super.delete(editora);
		super.getEntityManager().getTransaction().commit(); 
	}
}
